<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

//后台商品控制器
class GoodsController extends Controller {

    public function index() {
        $data = \DB::select('select types.*,concat(path,id) p from types order by p'); 
        return view('goods.goods_category')->with("data", $data);
    }

    public function edit($id) {
        $data = \DB::table("types")->where('id', $id)->first();
        $info = \DB::table("types")->where("id", $data->pid)->first();
        $all=\DB::table('types')->get();
        return view("goods.edit_category")->with("data", $data)->with("info", $info)->with("all",$all);
    }

    //子分类添加页面
    public function show($id) {
        $data = \DB::table("types")->where("id", $id)->first();
        return view('goods.son_category')->with("data", $data);
    }

    //后台添加商品分类页面 admin/admin/create 
    public function create() {
        $data = \DB::table("types")->get();
        return view('goods.add_category')->with("data", $data);
    }

    //插入操作 post admin/goods_category 
    public function store(Request $request) {
        //获取数据
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
//        print_r($arr);
//        exit;
        if (\DB::table("types")->insert($arr)) {
            return 1;
        } else {
            return 0;
        }
    }

    //后台删除商品分类页面 admin/admin/{admin}
    public function destroy($id) {
        if (\DB::table("types")->where("id", $id)->delete()) {
            return 1;
        } else {
            return "删除失败";
        }
    }

    //修改是否显示的状态
    public function ExitStatus(Request $request) {
        $arr = $request->except("_token");
//         var_dump($arr);
//         exit;
        if (\DB::table("types")->where("id", $arr['id'])->update($arr)) {
            return 1;
        } else {
            return "修改失败";
        }
    }

    //后台修改商品分类页面 admin/admin/{admin}
    public function update(Request $request) {
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        $arr['path']=$arr['pid'].",";
        $dat=explode(",", $arr['pid']); 
        $dat=$dat[count($dat)-1];
        $arr['pid']=$dat; 
        if (\DB::table("types")->where("id", $arr['id'])->update($arr)) {
            return 1;
        } else {
            return "修改失败";
        }
    }

    public function all_del(Request $request){
         $ids=$_POST['ids'];
         if(\DB::delete("delete from types where id in(".$ids.")")){
             return 1;
         }else{
             return "删除选中栏目失败";
         }
    }

//
//    +--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------------+--------------+
//| Domain | Method    | URI                                        | Name                   | Action                                                | Middleware   |
//+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------------+--------------+
// 
//|        | GET|HEAD  | admin/goods_category                       | goods_category.index   | App\Http\Controllers\Admin\GoodsController@index      | web          |
//|        | POST      | admin/goods_category                       | goods_category.store   | App\Http\Controllers\Admin\GoodsController@store      | web          |
//|        | GET|HEAD  | admin/goods_category/create                | goods_category.create  | App\Http\Controllers\Admin\GoodsController@create     | web          |
//|        | DELETE    | admin/goods_category/{goods_category}      | goods_category.destroy | App\Http\Controllers\Admin\GoodsController@destroy    | web          |
//|        | GET|HEAD  | admin/goods_category/{goods_category}      | goods_category.show    | App\Http\Controllers\Admin\GoodsController@show       | web          |
//|        | PUT|PATCH | admin/goods_category/{goods_category}      | goods_category.update  | App\Http\Controllers\Admin\GoodsController@update     | web          |
//|        | GET|HEAD  | admin/goods_category/{goods_category}/edit | goods_category.edit    | App\Http\Controllers\Admin\GoodsController@edit       | web          |
//|        | GET|HEAD  | api/user                                   |                        | Closure                                               | api,auth:api |
//+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------------+--------------+
}
